<?php

namespace App\Models\Car;

use App\Models\Constant\CarConstant;
use App\Models\User\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Models\Common\CommonModel;
class FixCarOrder extends CommonModel
{
    use HasFactory;

    protected $table = 'fix_car_order';
    protected $appends = ['status_name'];

    public function getImgsAttribute($val){
        $temp = [];
        foreach (array_filter(explode(',', $val)) as $item) {
            $temp[] = asset($item);
        }
        return $temp;
    }
    public function getFixImgsAttribute($val){
        $temp = [];
        foreach (array_filter(explode(',', $val)) as $item) {
            $temp[] = asset($item);
        }
        return $temp;
    }


    public function getStatusNameAttribute(){
        return CarConstant::FIX_ORDER[$this->status]??'未知';
    }

    public function getUser(){
        return $this->hasOne(User::class,'id','user_id');
    }

    public function getLst(array $conditions = []){
        $records = $this->newQuery();
        if(!empty($conditions['user_name'])){
            $records = $records->whereHas('getUser', function ($q) use ($conditions){
                $q->where('name','like','%'.$conditions['user_name'].'%');
            });
        }
        if(!empty($conditions['phone'])){
            $records = $records->whereHas('getUser', function ($q) use ($conditions){
                $q->where('phone',$conditions['phone']);
            });
        }
        if(!empty($conditions['rent_unit_id'])){
            $records = $records->where('rent_unit_id',$conditions['rent_unit_id']);
        }
        if(!empty($conditions['car_no'])){
            $records = $records->where('car_no',$conditions['car_no']);
        }
        return $records->orderByDesc('id');

    }

}
